package com.xm.xmapp.tools;

import java.lang.reflect.Field;

import android.app.Activity;
import android.os.Build;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;

public class StatusBarTool {

	/**
	 * 
	 * 为DrawerLayout 布局设置状态栏变色
	 * 
	 * 
	 * 
	 * @param activity
	 *            需要设置的activity
	 * 
	 * @param drawerLayout
	 *            DrawerLayout
	 * 
	 * @param color
	 *            状态栏颜色值
	 */

	public static void setColorForDrawerLayout(Activity activity,
			DrawerLayout drawerLayout, int color) {

		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

			activity.getWindow().addFlags(
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

			// 生成一个状态栏大小的矩形

			View statusBarView = createStatusBarView(activity, color);

			// 添加 statusBarView 到布局中

			ViewGroup contentLayout = (ViewGroup) drawerLayout.getChildAt(0);

			contentLayout.addView(statusBarView, 0);

			// 内容布局不是 LinearLayout 时,设置padding top

			if (!(contentLayout instanceof LinearLayout)
					&& contentLayout.getChildAt(1) != null) {
				int statusBarHeight = 0;
				try {

					Class<?> c = Class.forName("com.android.internal.R$dimen");
					Object obj = c.newInstance();
					Field field = c.getField("status_bar_height");
					int x = Integer.parseInt(field.get(obj).toString());
					statusBarHeight = activity.getResources()
							.getDimensionPixelSize(x);
				} catch (Exception e) {
					
				}

				contentLayout.getChildAt(1)
						.setPadding(0, statusBarHeight, 0, 0);

			}

			// 设置属性

			ViewGroup drawer = (ViewGroup) drawerLayout.getChildAt(1);

			drawerLayout.setFitsSystemWindows(false);

			contentLayout.setFitsSystemWindows(false);

			contentLayout.setClipToPadding(true);

			drawer.setFitsSystemWindows(false);

		}

	}

	/**
	 * 
	 * 为 DrawerLayout 布局设置状态栏透明
	 * 
	 * 
	 * 
	 * @param activity
	 *            需要设置的activity
	 * 
	 * @param drawerLayout
	 *            DrawerLayout
	 */

	public static void setTranslucentForDrawerLayout(Activity activity,
			DrawerLayout drawerLayout) {

		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

			// 设置状态栏透明

			activity.getWindow().addFlags(
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

			// 设置内容布局属性

			ViewGroup contentLayout = (ViewGroup) drawerLayout.getChildAt(0);

			contentLayout.setFitsSystemWindows(true);

			contentLayout.setClipToPadding(true);

			// 设置抽屉布局属性

			ViewGroup vg = (ViewGroup) drawerLayout.getChildAt(1);

			vg.setFitsSystemWindows(false);

			// 设置 DrawerLayout 属性

			drawerLayout.setFitsSystemWindows(false);

		}

	}

	/**
	 * 
	 * 生成一个和状态栏大小相同的矩形条
	 * 
	 * 
	 * 
	 * @param activity
	 *            需要设置的activity
	 * 
	 * @param color
	 *            状态栏颜色值
	 * 
	 * @return 状态栏矩形条
	 */

	private static View createStatusBarView(Activity activity, int color) {

		// 获得状态栏高度

		int resourceId = activity.getResources().getIdentifier(
				"status_bar_height", "dimen", "android");

		int statusBarHeight = activity.getResources().getDimensionPixelSize(
				resourceId);

		// 绘制一个和状态栏一样高的矩形

		View statusView = new View(activity);

		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				ViewGroup.LayoutParams.MATCH_PARENT,

				statusBarHeight);

		statusView.setLayoutParams(params);

		statusView.setBackgroundColor(color);

		return statusView;

	}
}
